import http from "@/axios/index.js";
import { handleError, handleSuccess } from "@/utils/responseHandler.js";

export const RoleApi = {

    getRoleList: () => http.get('/role/list')
        .then(res => handleSuccess(res, '角色列表获取'))
        .catch(error => handleError(error, '获取失败')),


    // 创建角色（仅传递name、code、dataScope）
    createRole: (data) => http.post('/role/create', {
        name: data.name,
        code: data.code,
        dataScope: data.dataScope
    })
        .then(res => handleSuccess(res, '角色创建成功'))
        .catch(error => handleError(error, '角色创建失败')),

    // 更新角色（传递id、name、code、dataScope）
    updateRole: (data) => http.put('/role/update', {
        id: data.id,
        name: data.name,
        code: data.code,
        dataScope: data.dataScope
    })
        .then(res => handleSuccess(res, '角色更新成功'))
        .catch(error => handleError(error, '角色更新失败')),

    // 删除角色
    deleteRole: (id) => http.delete(`/role/delete/${id}`)
        .then(res => handleSuccess(res, '角色删除成功'))
        .catch(error => handleError(error, '角色删除失败')),

    // 获取角色详情
    getRoleDetail: (id) => http.get(`/role/detail/${id}`)
        .then(res => handleSuccess(res, '角色详情获取成功'))
        .catch(error => handleError(error, '角色详情获取失败')),

    // 分配权限（参数改为roleId和aclIdList）
    assignAcl: (data) => http.post('/role/assignAcl', {
        roleId: data.roleId,
        aclIdList: data.aclIdList
    })
        .then(res => handleSuccess(res, '权限分配成功'))
        .catch(error => handleError(error, '权限分配失败'))
}